{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 第一部分：<code>map()</code> 和 <code>reduce()</code> 函数的使用\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "###  练习1 ：练习编写一个str to int 的函数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "def str_to_int(c):\n",
    "    def sum_10(x,y):\n",
    "        return 10*x+y\n",
    "    def char_to_num(c):\n",
    "        return {'0':0,'1':1,'2':2,'3':3,'4':4,'5':5,'6':6,'7':7,'8':8,'9':9}[c]\n",
    "    return reduce(sum_10, map(char_to_num, c))    "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "1357"
      ]
     },
     "execution_count": 25,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "str_to_int('1357')\n",
    "## 注意：代码最后一行 map 函数在调用自定义函数 char_to_num 时，只写函数名，不写参数。\n",
    "## 调用时只写函数名，如果加上参数代码就会直接执行，而此时我们只希望调用，不希望执行"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 练习2 : 利用<code>map()</code>函数，把用户输入的不规范的英文名，变为首字母大写，其他小写的规范名"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 70,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['Adam', 'Lisa', 'Bart']"
      ]
     },
     "execution_count": 70,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "## 方法一：利用 str.title 函数\n",
    "def co_name(name):\n",
    "    return map(str.title,name)\n",
    "co_name(['adam','LISA','barT']) #输入必须是列表list形式"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 71,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['Adam', 'Lisa', 'Bart', 'Ghgh']"
      ]
     },
     "execution_count": 71,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "## 方法二：编写函数\n",
    "def re_name(name1):\n",
    "    return map(lambda x:x[0].upper()+x[1:].lower(),name1)\n",
    "re_name(['adam','LISA','barT','GhGh'])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 练习3：<code>sum()</code>函数可以接受list并求和，编写一个<code>prod()</code>函数，可以接受list并利用reduce()求积"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 74,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "240"
      ]
     },
     "execution_count": 74,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "def prod(num):\n",
    "    def mul(x,y):\n",
    "        return x*y\n",
    "    return reduce(mul,num)\n",
    "prod([2,4,5])\n",
    "\n",
    "#如果使用lambda\n",
    "def prod1(n):\n",
    "    return reduce(lambda x,y:x*y,n)\n",
    "prod1([2,4,5,6])    "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 总结：<code>map,reduce,lambda(匿名函数)</code> 函数均可以使代码更加简洁，但也可以通过定义函数实现"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "collapsed": true
   },
   "source": [
    "## 第二部分 <code>filter，sorted</code>函数的使用"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 练习一：编写一个筛选奇数的函数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[1, 5, 9]"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "def is_odd(num):\n",
    "    return num%2==1\n",
    "filter(is_odd,[1,5,6,9,12])\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 练习二：编写一个函数筛选0-50之间的质数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47]"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "def is_pri(num):\n",
    "    if num<=1:\n",
    "        return False\n",
    "    for n in range(2,num):\n",
    "        if num%n==0:\n",
    "            return False\n",
    "    return True\n",
    "filter(is_pri, range(50))\n",
    "    \n",
    "        "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 练习三： <code>sorted</code>函数即是排序函数，可以对列表里面的数字和字符串进行排序。sorted 函数和map 等函数不同的地方在于第一个元素是变量，第二个是函数。\n",
    "### 编写一个函数对字符串按照首字母从a到z进行排序（忽略大小写）"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['AAsj', 'tHu', 'thu', 'Ujs']"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "def str_sort(s1,s2):\n",
    "    u1=s1.upper()\n",
    "    u2=s2.upper()\n",
    "    if u1<u2:\n",
    "        return -1\n",
    "    elif u1>u2:\n",
    "        return 1\n",
    "    return 0\n",
    "sorted(['tHu','Ujs','AAsj','thu'],str_sort)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 第三"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 2",
   "language": "python",
   "name": "python2"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.13"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
